﻿@model ImportProfileListModel

@{
	var hasProfiles = Model.Profiles.Any();

    ViewBag.Title = T("Common.Import").Value;
}

<div class="section-header">
    <div class="title">
        <i class="fa fa-download"></i>
        <span>@T("Common.Import")</span>
    </div>
    <div class="options">
		<a href="#" data-url="@Url.Action("Create")" class="btn btn-primary import-profile-button">
			<i class="fa fa-plus"></i>
			<span>@T("Admin.DataExchange.AddNewProfile")</span>
		</a>
    </div>
</div>

<div sm-if="!hasProfiles" class="alert alert-info">
	@T("Admin.DataExchange.Import.NoProfiles")
</div>
<div sm-if="hasProfiles" class="table-responsive">
    <table id="import-profile-list" class="table admin-table">
		<thead>
			<tr>
				<th>@T("Admin.DataExchange.Import.Name")</th>
				<th>@T("Admin.Common.Entity")</th>
				<th>@T("Admin.Common.ImportFiles")</th>
				<th>@T("Admin.DataExchange.Import.LastImportResult")</th>
				<th>@T("Admin.System.ScheduleTasks.LastStart")</th>
				<th>@T("Admin.System.ScheduleTasks.NextRun")</th>
				<th>@T("Admin.Common.Actions")</th>
			</tr>
		</thead>
		<tbody>
			@foreach (var profile in Model.Profiles)
			{
				<tr data-profile-id="@profile.Id" data-task-id="@profile.TaskId" attr-class='(!profile.Enabled, "disabled")'>
					<td>
						<a asp-action="Edit" asp-route-id="@profile.Id"><strong>@profile.Name</strong></a>
					</td>
					<td>
						@profile.EntityTypeName
					</td>
					<td>
						<ul sm-if="profile.ExistingFiles.Any()" class="fa-ul mb-0 ml-0">
							@foreach (var file in profile.ExistingFiles.Where(x => !x.RelatedType.HasValue))
							{
								<li title="@file.File.Name">
									<file-icon file-extension="@file.File.Extension" />
									<a asp-action="DownloadImportFile" asp-route-id="@profile.Id" asp-route-name="@file.File.Name" download>
										@file.File.Name
									</a>
								</li>
							}
						</ul>
					</td>
					<td id="ProfileImportResult@(profile.Id)">
						@if (profile.ImportResult != null)
						{
							<partial name="ProfileImportResult" model="profile.ImportResult" />
						}
						else
						{
							<span class="text-muted">@string.Empty.NaIfEmpty()</span>
						}
					</td>
					<td>
						<div class="last-run-info">
							<partial name="~/Areas/Admin/Views/Scheduling/_LastRun.cshtml" model="profile.TaskModel" />
						</div>
					</td>
					<td class="progress-info">
                        <div class="next-run-info">
							<partial name="~/Areas/Admin/Views/Scheduling/_NextRun.cshtml" model="profile.TaskModel" />
                        </div>
						<div class="task-progress text-muted" 
							attr-class='(!profile.IsTaskRunning, "hide")'
							data-task-id="@profile.TaskId" 
							data-running="@profile.IsTaskRunning.ToString().ToLower()"></div>
					</td>
					<td class="omega" style="width: 200px;">
                        <button type="button" class="btn btn-block btn-sm btn-warning btn-run-task"
							attr-class='(profile.IsTaskRunning || !profile.Enabled, "hide")'
							data-url="@Url.Action("Execute", "Import", new { area = "Admin", id = profile.Id })"
							data-task-id="@profile.TaskId">
								<i class="fa fa-play"></i>
								<span>@T("Admin.System.ScheduleTasks.RunNow")</span>
                        </button>
                        <a asp-action="CancelJob" asp-controller="Scheduling" asp-route-id="@profile.TaskId" asp-area="Admin"
							class="btn btn-block btn-sm btn-secondary btn-cancel-task confirm"
							attr-class='(!profile.IsTaskRunning, "hide")'
							data-task-id="@profile.TaskId">
								<span>@T("Common.Cancel")</span>
                        </a>
                        <a asp-action="DownloadLogFile" asp-route-id="@profile.Id"
							target="LogFile"
							class="btn btn-block btn-sm btn-secondary btn-log"
							attr-class='(profile.IsTaskRunning || !profile.Enabled || !profile.LogFileExists, "d-none")'>
								<i class="far fa-file-lines"></i>
								<span>@T("Admin.Configuration.ActivityLog")</span>
                        </a>
					</td>
				</tr>
			}
		</tbody>
    </table>
</div>

<modal id="import-profile-dialog" sm-render-at-page-end="true" sm-size="Large">
	<modal-header sm-title="@T("Admin.DataExchange.AddNewProfile")"></modal-header>
    <modal-body>
		<form asp-action="Create" method="post" class="import-profile-form">
			<p class="text-muted">
				@T("Admin.DataExchange.Import.ProfileCreationNote")
			</p>
			<div>
				<div class="form-group">
					<smart-label asp-for="EntityType" />
					<select asp-for="EntityType" asp-items="Html.GetLocalizedEnumSelectList(typeof(ImportEntityType))"></select>
				</div>
				<div id="ImportFileUploadContainer" class="form-group">
					<input type="hidden" name="TempFileName" />
					<file-uploader 
						file-uploader-name="ImportFileUploader"
						upload-url='@Url.Action("FileUpload", new { id = Model.Id })'
						type-filter=".csv,.xlsx,.txt,.tab"
						display-browse-media-button="false"
						display-remove-button="true"
						display-remove-button-after-upload="true"
						upload-text='@T("Admin.DataExchange.Import.FileUpload")'
						onuploadcompleted="onUploadCompleted" />
				</div>
				<div id="ImportFileList" class="hide">
					&nbsp;
				</div>
			</div>
		</form>
	</modal-body>
    <modal-footer>
		<button class="btn btn-secondary btn-flat" data-dismiss="modal"><span>@T("Common.Cancel")</span></button>
		<button class="btn btn-primary" disabled="disabled"><span>@T("Common.OK")</span></button>
    </modal-footer>
</modal>

<script sm-target-zone="scripts" data-origin="ImportList">
    window['onUploadCompleted'] = function (file, response) {
        if (!_.isEmpty(response.name)) {
            $('#import-profile-dialog').find('input[name=TempFileName]').val(response.name);
			$('#import-profile-dialog').find('.btn-primary').prop('disabled', false);
		}
        else if (!_.isEmpty(response.error)) {
            $('#ImportFileList').html('<div class="alert alert-danger">' + response.error + '</div>').show();
		}
	}

    $(function() {
		// Show profile dialog.
		$(document).on('click', '.import-profile-button', function (e) {
			e.preventDefault();
			$('#import-profile-dialog').modal();
			return false;
        });

		// Submit dialog form.
        $('#import-profile-dialog').on('click', '.btn-primary', function () {
			$('#import-profile-dialog').find('.import-profile-form').submit();
		});

		// Start export task.
		$(document).on('click', '.btn-run-task', function (e) {
			e.preventDefault();
            $(this).postData({
                ask: @T("Admin.Common.AskToProceed").JsValue
			});
			return false;
		});

		Smartstore.Admin.TaskWatcher.startWatching({
			context: $('#import-profile-list'),
			elementsSelector: '.task-progress',
			defaultProgressMessage: @T("Admin.System.ScheduleTasks.RunNow.IsRunning").JsValue,
			pollUrl: '@Url.Action("GetRunningTasks", "Scheduling")',
			onTaskStarted: function (task, el) {
				var tr = el.closest('tr');
				tr.find('.btn-run-task').addClass('hide');
				tr.find('.btn-cancel-task').removeClass('hide');
				tr.find('.btn-log').addClass('hide');
				tr.find('.next-run-info').addClass('hide');
			},
			onTaskCompleted: function (taskId, el) {
				var tr = el.closest('tr');
				var profileId = tr.data('profile-id');

				tr.find('.btn-run-task').removeClass('hide');
				tr.find('.btn-cancel-task').addClass('hide');
				tr.find('.btn-log').removeClass('hide');

				$.ajax({
					cache: false,
					global: false,
					type: 'POST',
					url: '@Url.Action("GetTaskRunInfo", "Scheduling")',
					data: { id: taskId },
					success: function (data) {
						tr.find('.next-run-info').html(data.nextRunHtml).removeClass('hide');
						tr.find('.last-run-info').html(data.lastRunHtml);
					}
				});

				$.get('@Url.Action("ProfileImportResult")?profileId=' + profileId, function (response) {
					$('#ProfileImportResult' + profileId).html(response || '');
				});
			}
		});
    });
</script>